/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package datos;
 
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import logica.ManejadorTop5;
import logica.Top5;
import org.primefaces.event.ItemSelectEvent;
 

import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;


 
/**
 *
 * @author Karadak
 */
 
@ManagedBean
public class ChartView implements Serializable {
    
    
 
    private BarChartModel barModel;
    private PieChartModel pieModel1;
    private ArrayList<String> nombre = new ArrayList();
 
    @PostConstruct
    public void init() {
        createBarModels();
        createPieModels();
    }
 
    public BarChartModel getBarModel() {
        return barModel;
    }
     
    public PieChartModel getPieModel1() {
        return pieModel1;
    }
 
    private void createBarModels() {
        createBarModel();
    }
     
    private void createBarModel() {
        barModel = initBarModel();
         
        barModel.setTitle("Top 5");
        barModel.setLegendPosition("ne");
//        barModel.setAnimate(true);
        barModel.setShadow(true);
       
         
        Axis xAxis = barModel.getAxis(AxisType.X);
        xAxis.setLabel("Restaurantes");
         
        Axis yAxis = barModel.getAxis(AxisType.Y);
        yAxis.setLabel("Punteo");
        
        yAxis.setMin(0);
        yAxis.setMax(5);
    }
     
    private BarChartModel initBarModel() {
        BarChartModel model = new BarChartModel();
        
        ManejadorTop5 top = new ManejadorTop5();
        ArrayList<Top5> lista = top.getTopFive();
        Iterator<Top5> i = lista.iterator();
        
        ChartSeries restaurantes = new ChartSeries();
        restaurantes.setLabel("Restaurantes");        
        
        while(i.hasNext()){
            Top5 data = i.next();
            //pieModel1.set(data.getNombre_restaurante(), data.getPromedio());
            restaurantes.set(data.getNombre_restaurante(),  data.getPromedio());
            nombre.add(data.getNombre_restaurante());
        }
        
        model.addSeries(restaurantes);

         
        return model;
    }
 
    private void createPieModels() {
        createPieModel1();
    }
     
    private void createPieModel1() {
        pieModel1 = new PieChartModel();
        
        ManejadorTop5 top = new ManejadorTop5();
        ArrayList<Top5> lista = top.getTopFive();
        Iterator<Top5> i = lista.iterator();
        while(i.hasNext()){
            Top5 data = i.next();
            pieModel1.set(data.getNombre_restaurante(), data.getPromedio());
        }
       
         
//        pieModel1.setTitle("Top 5");
//        pieModel1.setSeriesColors("blue");
        pieModel1.setLegendPosition("w");
    }
 
    public void itemSelect(ItemSelectEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Top 5",
                         nombre.get(event.getItemIndex())+ ", Posición: " + (event.getItemIndex()+1) );
       
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
}